using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI {
	public class ProgramFileItem : MonoBehaviour {
		public TMP_Text FileNameText;

		public Button SelectItemButton;
		public Button RemoveButton;

		public Image StatusImage;
		
		public Sprite LoadedItemIcon;
		public Sprite DownloadItemIcon;
		public Sprite ActiveItemIcon;

		public string FileName { get; protected set; }

		UnitStatusWindow _owner;

		public void Setup(string fileName, bool downloaded, bool selected, UnitStatusWindow owner) {
			FileName = fileName;
			_owner= owner;
			FileNameText.text = fileName;
			RemoveButton.onClick.RemoveAllListeners();
			RemoveButton.onClick.AddListener(OnRemoveButtonClick);
			SelectItemButton.onClick.RemoveAllListeners();
			SelectItemButton.onClick.AddListener(OnItemClick);
			if (selected) {
				StatusImage.sprite = ActiveItemIcon;
			} else if ( downloaded ) {
				StatusImage.sprite = LoadedItemIcon;
			} else {
				StatusImage.sprite = DownloadItemIcon;
			}
		}

		void OnRemoveButtonClick() {
			if ( !_owner ) {
				return;
			}
			_owner.ShowRemoveFileDialog(FileName);
		}

		void OnItemClick() {
			if ( !_owner ) {
				return;
			}
			_owner.SetSelectedFile(FileName);
		}
	}
}